package com.elf.zero.app;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

/**
 * fragment基类
 */
public abstract class BaseFragment extends Fragment {

    protected View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView=inflater.inflate(getLayoutResId(),container,false);

        initView();
        loadData();
        return rootView;
    }

    /**
     * 获取指定ID的view
     */
    public <T extends View> T getViewById(int resId){
        return (T) rootView.findViewById(resId);
    }

    /**
     * 显示dialog消息
     */
    protected void showDialog(String text){
        if(getActivity()!=null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage(text);
            builder.setNegativeButton("确定", null);
            builder.show();
        }
    }

    /**
     * 显示toast
     */
    protected void showToast(String text) {
        if(getActivity()!=null) {
            Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 获取去布局资源ID
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 加载数据
     */
    protected abstract void loadData();

}
